<template>
  <div>
    <!--条件筛选-->
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="110" @filterChange="changeHandle"
               @onCollapse="collapseHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams2"
      isSelectColumn
      datakey="records"
      :onRowSelectChange="selectChange"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visibleForm" :title="dialogTitle" destroyOnClose width="70vw"
                :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <BatteryRecycleDetail :handleType="handleType" :pageType="pageType" :value="formData" @close="closeHandler"/>
    </BaseDialog>
    <BaseDialog :visible.sync="visibleForm2" :title="dialogTitle" destroyOnClose width="60vw"
                :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <Handover :value="formData" @close="closeHandler2"/>
    </BaseDialog>
  </div>
</template>

<script>
import {getBatteryRecycleList, postUpdateStatusByIds} from '@/api/partsmanage/batteryTrace/batteryRecycle';
import {getCurrentDealerBranch} from '@/api/partsmanage/batteryTrace/branchEdit';
import {dclxArr, hsnrArr, jjztArr, statusArr} from './constant';
import BatteryRecycleDetail from './batteryRecycleDetail';
import Handover from './handover';
import {dictionary} from '@/utils/dictMixin';

export default {
  name: "batteryRecycleIndex",
  components: {
    BatteryRecycleDetail,
    Handover,
  },
  mixins: [dictionary],
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchApi: getBatteryRecycleList,
      pageType: 'DLR1',
      fetchParams: {},
      visibleForm: false,
      visibleForm2: false,
      handleType: '',
      formData: {},
      dialogTitle: '电池回收',
      rowSelectData: [],
    };
  },
  computed: {
    fetchParams2() {
      return {
        pageType: this.pageType,
        ...this.fetchParams
      }
    }
  },
  methods: {
    createTopFilterList() {
      return [
        {type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入'},
        {type: 'INPUT', label: '电池编号', fieldName: 'batteryCode', placeholder: '请输入'},
        {type: 'INPUT', label: '电池模块编号', fieldName: 'batteryModuleCode', placeholder: '请输入'},
        {type: 'INPUT', label: '电芯编号', fieldName: 'batteryCellCode', placeholder: '请输入'},
        {type: 'SELECT', label: '回收内容', fieldName: 'batterySpecies', itemList: hsnrArr, placeholder: '请选择'},
        {type: 'SELECT', label: '状态', fieldName: 'status', itemList: statusArr, placeholder: '请选择'},
        {type: 'SELECT', label: '交接状态', fieldName: 'handOverStatus', itemList: jjztArr, placeholder: '请选择'},
        {
          type: 'RANGE_DATE',
          label: '创建日期',
          fieldName: 'createdAt1|createdAt2',
          valueFormat: 'yyyy-MM-dd',
          placeholder: '请选择'
        },
        {type: 'INPUT', label: '电池供应商', fieldName: 'batterySupplierName', placeholder: '请输入'},
      ]
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 200,
          fixed: 'left',
          render: props => {
            []
            return (
              <div>
                <el-button disabled={props.row.status !== 'C'} size="mini" type="text"
                           onClick={() => this.detailHandle(props.row, 'edit')}>
                  编辑
                </el-button>
                <el-divider direction="vertical"/>
                <el-button size="mini" type="text" onClick={() => this.detailHandle(props.row, 'show')}>
                  明细
                </el-button>
                <el-divider direction="vertical"/>
                <el-button
                  disabled={!(props.row.status === 'T' && (props.row.handOverStatus === 'W' || !props.row.handOverStatus))}
                  size="mini" type="text" onClick={() => this.handover(props.row)}>
                  回收交接
                </el-button>
                <el-divider direction="vertical"/>
                <el-button disabled={props.row.status !== 'C'} size="mini" type="text"
                           onClick={() => this.updateStatusHandle(props.row, 'F')}>
                  作废
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '回收登记单号',
          dataIndex: 'recycleCode',
          sorter: true,
        },
        {
          title: 'VIN',
          width: 150,
          dataIndex: 'vin',
          sorter: true,
        },
        {
          title: '回收内容',
          dataIndex: 'batterySpecies',
          width: 150,
          sorter: true,
          dictItems: hsnrArr,
        },
        {
          title: '回收日期',
          dataIndex: 'storageDate',
          sorter: true,
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: 150,
          sorter: true,
          dictItems: statusArr,
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          width: 150,
        },
        {
          title: '提交时间',
          dataIndex: 'subAt',
        },
        {
          title: '回收交接状态',
          dataIndex: 'handOverStatus',
          width: 150,
          sorter: true,
          dictItems: jjztArr,
        },
        {
          title: '回收交接时间',
          dataIndex: 'handOverAt',
        },
        {
          title: '回收确认时间',
          dataIndex: 'confirmAt',
        },
        {
          title: '电池包编码',
          dataIndex: 'batteryCode',
          sorter: true,
        },
        {
          title: '电池模块编号',
          dataIndex: 'batteryModuleCode',
          sorter: true,
        },
        {
          title: '电芯编号',
          dataIndex: 'batteryCellCode',
          sorter: true,
        },
        {
          title: '电池类别',
          dataIndex: 'batteryType',
          sorter: true,
          dictItems: dclxArr,
        },
        {
          title: '电池质量',
          dataIndex: 'batteryWeight',
          sorter: true,
        },
        {
          title: '电池年限',
          dataIndex: 'batteryYear',
          sorter: true,
        },
        {
          title: '电池供应商',
          dataIndex: 'batterySupplier.name',
          sorter: true,
        },
      ]
    },
    async addHandle() {
      let res = await getCurrentDealerBranch({});
      if (res.data.length < 1) {
        return this.$notify.error({title: '提示信息', message: '当前经销商非电池回收网点，无法新增！'});
      }
      this.handleType = 'add';
      this.visibleForm = true
    },
    detailHandle(row, handleType) {
      this.handleType = handleType;
      this.formData = {
        id: row.id,
        data: {
          ...row
        }
      };
      this.visibleForm = true
    },
    handover(row) {
      this.formData = {
        id: row.id,
        data: {
          ...row
        }
      };
      this.visibleForm2 = true
    },
    updateStatusHandle(row, status) {
      this.$confirm('确认作废吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async r => {
        const res = await postUpdateStatusByIds({
          status,
          ids: [row.id]
        })
        if (res.resultCode == 200) {
          this.$notify.success({title: '提示信息', message: '操作成功'});
          // 重新加载
          this.fetchParams = Object.assign({}, this.fetchParams);
        }
      })
    },
    closeHandler(type) {
      this.visibleForm = false
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    closeHandler2(type) {
      this.visibleForm2 = false
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    selectChange(val) {
      this.rowSelectData = val;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
  }
}
</script>

<style scoped>

</style>
